# -*- coding: utf-8 -*-
'''
Created on 2017年2月27日

@author: ZhuJiahui
'''

from operator import itemgetter

def count_tf(text_corpus, word_list):
    '''
    结合给定词汇列表计算文档中这些词汇的频数
    :param text_corpus: 文档集合 (2d string list)
    :param word_list: 预先给定的词汇列表 (1d string list)
    :return 词汇以及对应的词频的元胞列表 (1d (string, int) list)
    '''
    
    tf_dict = {}  # 词频TF字典
    for each_key in word_list:
        tf_dict[each_key] = 0

    for i in range(len(text_corpus)):
        for j in range(len(text_corpus[i])):
            try:
                tf_dict[text_corpus[i][j]] += 1
            except:
                tf_dict[text_corpus[i][j]] = 0
    
    # 词频列表
    tf_value_list = [tf_dict[each_key] for each_key in word_list]
    # 按词频降序排序
    combined_wl_tf = zip(word_list, tf_value_list)
    combined_wl_tf = sorted(combined_wl_tf, key=itemgetter(1), reverse=True)
    
    return combined_wl_tf

if __name__ == '__main__':
    pass